// 2013-9-25下午11:29:16  net.betou.trade.admin.action.SharingSettingsAct.java

package net.betou.trade.admin.action;

import java.util.HashSet;
import java.util.Set;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import net.betou.common.util.TradeUtils;
import net.betou.core.entity.ConfigLogin;
import net.betou.trade.action.abs.ActionSupport;
import net.betou.trade.entity.main.AgentAndAdmins;
import net.betou.trade.entity.main.Role;
import net.betou.trade.entity.main.Share;
import net.betou.trade.main.manager.role.AgencyRoleMng;
import net.betou.trade.web.Messages;
import net.betou.trade.web.WebErrors;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;

/**
 * 共享设置
 * 1. 代理商的共享设置默认勾选喊单、新闻、公告
 *     在创建代理商的时候，就应该设置这些 
 * @author 叶装装
 * @version 1.1
 */
@Controller
public class SharingSettingsAct extends ActionSupport< Share >{
	
	/**
	 * 系统管理员是根据ID，来配置共享信息，非系统管理员则是根据自己的角色来进行判断
	 * 
	 * @param id
	 * 					角色ID
	 * @param request
	 * @param response
	 * @param model
	 * @return
	 */
	@RequestMapping( "/share/v_add.jspx" )
	public String v_add( Integer id, String success, HttpServletRequest request, HttpServletResponse response, ModelMap model ){
		
		AgentAndAdmins admin = TradeUtils.getAdmin( request );
		
		WebErrors errors = validateSetting( id, admin );
		
		if( errors.hasErrors( ) ){
			return errors.showErrorPage( model );
		}
		
		Role role = mng.findRole( admin, id ); // 查找角色的共享设置...
		
		model.addAttribute( "exts", ConfigLogin.roleExtMap );
		model.addAttribute( "role", role );
		model.addAttribute( "success", success );
		return getAddPage( );
	}
	
	/**
	 * @version 1.1
	 * @param id
	 * 					角色ID
	 * @param request
	 * @param model
	 * @return
	 */
	@RequestMapping( "/share/o_save.jspx" )
	public String o_save( Integer id, String [ ] ext, HttpServletRequest request, ModelMap model ){
		
		AgentAndAdmins admin = TradeUtils.getAdmin( request );
		
		WebErrors errors = validateSetting( id, admin );
		
		if( errors.hasErrors( ) ){
			return errors.showErrorPage( model );
		}
		
		// 事先要进行角色的判断， 查看该ID是否存在，不过懒得写了
		Role bean = mng.findRole( admin, id );
		
		mng.update( bean, splitShares( ext ) );
		
		return "redirect:v_add.jspx?id=" + id + "&success=y";
	}
	
	private WebErrors validateSetting( Integer id, AgentAndAdmins admin ){
		WebErrors errors = WebErrors.create( );
		
		if( id == null || id == 0 ){
			errors.addError( Messages.DATA_NOT_EXIST );
			return errors;
		}
		
		Role role = mng.findRole( admin, id ); // 查找角色的共享设置...
		
		if( role == null ){
			errors.addError( Messages.DATA_NOT_EXIST );
			return errors;
		}
		
		return errors;
		
	}
	
	private Set< String > splitShares( String [ ] shares ){
		Set< String > shareSet = new HashSet< String >( );
		
		if( shares == null || shares.length == 0 ){
			return shareSet;
		}
		
		for( String s : shares ){
			shareSet.add( s );
		}
		
		return shareSet;
		
	}
	
	@Autowired
	private AgencyRoleMng mng;
	
	public static final String DIRECTORY = "share";
	
	@Override
	protected String getDirectory( ) {
		return DIRECTORY;
	}

}
